package cn.kgc.tangcco.tcbd1011.no13.user.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.ant.SessionsTask;

import com.alibaba.fastjson.JSON;
import com.sun.java.swing.plaf.windows.WindowsRadioButtonMenuItemUI;

import cn.kgc.tangcco.tcbd1011.no13.base.action.BaseServlet;
import cn.kgc.tangcco.tcbd1011.no13.base.properties.factory.PropertiesFactory;
import cn.kgc.tangcco.tcbd1011.no13.base.util.Email;
import cn.kgc.tangcco.tcbd1011.no13.base.util.MyBeanUtils;
import cn.kgc.tangcco.tcbd1011.no13.base.util.Utils;
import cn.kgc.tangcco.tcbd1011.no13.user.entity.User;
import cn.kgc.tangcco.tcbd1011.no13.user.service.UserService;
import sun.print.resources.serviceui;
@WebServlet(name="UserServlet",urlPatterns="/user.action")
public class UserServlet extends BaseServlet {

	private static final String SUCCESS="user.action?methodName=doListUser";
	private UserService uservice;
	public UserServlet() {
		uservice=(UserService) PropertiesFactory.getInstance(Utils.USERSERVICE);
	}
//	跳转装list页面  显示所有用户信息
	public String doListUser(HttpServletRequest request, HttpServletResponse response) {
		String forward = "user/listUser";
		User user=new User();
		request.setAttribute("ulist", uservice.queryAllUser(user));
		return forward;
	}
//	跳转到增加页面  
	public String toAddUser(HttpServletRequest request, HttpServletResponse response) {
		String forward = "user/addUser";
		
		return forward;
	}
//	去添加页面  添加值
	public String doAddUser(HttpServletRequest request, HttpServletResponse response) {
		String forward = SUCCESS;
		request.setAttribute("rows", uservice.addUser(MyBeanUtils.populate(User.class, request.getParameterMap())));
		
		return forward;
	}
//	跳转到按主键查看页面
	public String detailUser(HttpServletRequest request, HttpServletResponse response) {
		String forward = "user/detailUser";
		User user = new User();
		user.setUserName(request.getParameter("userName"));
		user.setEmail(request.getParameter("email"));
		user.setUserId(Utils.parseInt(request.getParameter("id")));
		String detailOrUpdate=request.getParameter("detailOrUpdate");
		if(detailOrUpdate.equals("detail")) {
			forward="user/detailUser";
		}else {
			forward="user/updateUser";
		}
		request.setAttribute("users", uservice.detailUser(user));
		return forward;
	}
//	去修改页面
	public String doUpdateUser(HttpServletRequest request, HttpServletResponse response) {
		String forward = SUCCESS;
		request.setAttribute("rows", uservice.updateUser(MyBeanUtils.populate(User.class, request.getParameterMap())));
		return forward;
	}
//	删除页面
	public String doDeleteUser(HttpServletRequest request, HttpServletResponse response) {
		String forward = SUCCESS;
		request.setAttribute("rows", uservice.deleteUser(Utils.parseInt(request.getParameter("userId"))));
		return forward;
	}
	
//	查看用户名是否存在
	public String ajaxResetPassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String uname=request.getParameter("userName");
		String email=request.getParameter("email");
		User user=new User();
		
		if(uname!=null && !uname.isEmpty()) {
			user.setUserName(uname);
		}
		if(email!=null && !email.isEmpty()) {
			user.setEmail(email);
		}
		User users = uservice.detailUser(user);
		
		String result = JSON.toJSONString(users);
		response.setContentType("text/json;charset:utf-8");
		PrintWriter out = response.getWriter();
		out.print(result);
		out.flush();
		out.close();
		
		return Utils.AJAX;
	}
	
//	邮箱验证码
	public String yzEmail(HttpServletRequest request, HttpServletResponse response) throws IOException  {
		HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		
		String email = request.getParameter("email");
//		System.out.println("em====="+email);
		Email em = new Email();
		String m = em.sendEamilCode(email);
//		response.setContentType("text/json;charset:utf-8");
		if(m!=null) {
			session.setAttribute("yz", m);
		}
		
		if("错误".equals(m)) {
			out.print("cuowu");
		}else {
			out.print(m);
		}
		System.out.println("m===="+m);
//		session.invalidate();
//		session.removeAttribute("yz");
//		out.close();
//		out.flush();
//		out.close();
		return Utils.AJAX;
	}
	
	public String toUpdatePassword(HttpServletRequest request, HttpServletResponse response) {
		String forward = "user/updatePassword";
		User user = new User();
		user.setUserName(request.getParameter("userName"));
		request.setAttribute("user", uservice.detailUser(user));
		return forward;
	}
	
	public String toUpdateNext(HttpServletRequest request, HttpServletResponse response) throws IOException {
//		PrintWriter out = response.getWriter();
		String forward = "user/next3";
		/*String uname=request.getParameter("userName");
		Utils.println(uname);
//		String email = request.getParameter("email");
		HttpSession session = request.getSession();
		String email = (String) session.getAttribute("email");
		String yz=request.getParameter("yanzheng");
		Utils.println("email===="+email);
		Utils.println("yz=="+yz);
		if(email!=null) {
//			System.out.println(email);
			Email em = new Email();
			String m = em.sendEamilCode(email);
			request.setAttribute("yzm", m);
			Utils.println("m-=---"+m);
//			session.setAttribute("yanzheng", m);
		}*/
		return forward;
	}
	
	public String doUpdatePassword(HttpServletRequest request, HttpServletResponse response) {
		String forward="doLogin.action?methodName=toLogin";
		User user = new User();
		user.setUserName(request.getParameter("userName"));
//		user.setUserName(request.getParameter("userName"));
//		user.setUserId(Utils.parseInt(request.getParameter("userId")));
		user.setPassword(request.getParameter("password"));
		request.setAttribute("user", uservice.detailUser(user));
		request.setAttribute("rows", uservice.updatePassord(user));
		return forward;
	}
	
	
	
}
